// pages/cart/cart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {


      goodslist:[

      ],
      shifou:false,
      xuanze:false,
      gouxuanlist:[],
      weifulist:[],

        goodsArr:[
            
          ],
          totalData:0,
          boolen: false,
          goodsReverse: [],

    },
    dianji() {
      this.setData({
        boolen: !this.data.boolen,
      })
      console.log(this.data.boolen);
      if (this.data.boolen) {
        this.setData({
          goodsReverse: this.data.goodsArr.reverse(),
        })
      } else {
        this.setData({
          goodsArr: this.data.goodsArr.reverse()
        })
      }
    },
    getTotal(options){
   
      let goodslist = this.data.goodslist;
      let obj=this.data.goodslist.filter(item=>item.boolen==true);

      let weifu=this.data.goodslist.filter(item=>item.boolen==false);

     this.setData({
       gouxuanlist:obj,
       weifulist:weifu,
     })

      console.log(obj);
      if(obj.length){
        let data = obj.reduce((total,item)=>{
          total =total + item.num * item.price;
          return total;
      },0)

      
      this.setData({
          totalData:data
      })
      console.log(this.data.totalData);
          return;

      }else if(goodslist.length){
        let data = goodslist.reduce((total,item)=>{
            total =total + item.num * item.price;
            return total;
        },0)

        
        this.setData({
            totalData:data
        })
    }
     
   

  },
  zhifu(){
    let arr=this.data.gouxuanlist;
      let newArr=this.data.weifulist;
    if(!this.data.gouxuanlist.length){
     
      this.setData({
        shifou:!this.data.shifou,

      })
      wx.setStorageSync("goodsgouxuan",arr);
      wx.setStorageSync('goodsweifu',newArr);
      return;
    }else{
      this.setData({
        xuanze:!this.data.xuanze,

      })

      
      
   wx.setStorageSync("goodsgouxuan",arr);
   wx.setStorageSync('goodsweifu',newArr);

    }
    

  },
  
    add(options){
         let index=options.target.dataset.index;
         let add=this.data.goodslist[index].num+1;
         let str="goodslist["+index+"].num";
         let obj={};
         obj[str]=add;
         this.setData(obj);
         this.getTotal();


    },
    delete(options){
       let index=options.target.dataset.index;
       let num=this.data.goodslist[index].num;
       let str=`goodslist[${index}].num`;
       if(num<=1){
         num=1;

       }else{
         num--;
       }
       let obj={};
       obj[str]=num;

       this.setData(obj);
       this.getTotal();
    },
    click(options){

      console.log(options);
        let index=options.currentTarget.dataset.index;
        this.data.goodslist.splice(index,1)[0];
     

      this.setData({
        goodslist:this.data.goodslist,
      })
      this.getTotal();


    },
    changeSelect(options){
      
      let id=options.currentTarget.dataset.index;
      console.log(id);
     
       this.setData({
         [`goodslist[${id}].boolen`]:!this.data.goodslist[id].boolen,
       })
       console.log(this.data.goodslist[id].boolen);
       this.getTotal();
       return id;
     
     


      
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.getGoodsData();
    },
    getGoodsData() {
      let _this = this;
      
  
      wx.request({
  
        url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search?"cid"=600&pagenum=1713&pagesize=30', //仅为示例，并非真实的接口地址
  
        header: {
          'content-type': 'application/json' // 默认值
        },
     
        success(res) {
          let goodslist = res.data.message.goods;
          let lists = [];
          goodslist.forEach(item => {
            let {
              goods_id,
              goods_name,
              goods_small_logo,
              goods_price
            } = item;
            let data = {
              id: goods_id,
              goods: goods_small_logo,
              name: goods_name,
              price: goods_price
  
            }
            if (goods_small_logo) {
              lists.push(data);
            }
  
  
          })
        
          _this.setData({
            goodsArr: lists,
           
  
          })
         
  
        }
      })
  
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
     let arr= wx.getStorageSync('GoodsCarList');
     this.setData({
       goodslist:arr,
      
     })
     console.log(arr);
     this.getTotal();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
      wx.setStorageSync('GoodsCarList',this.data.goodslist);
    
      console.log(wx.getStorageSync('GoodsCarList'))

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})